<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
  <style></style>
</head>

<body>
  <div id="app">
    <div>
      <div style="display: inline-block" v-for="(inex,id) in arr">
        <input type="checkbox" @click="txt(id)" />
        <span>{{inex}}</span>
      </div>
      <p>你选中的元素, 累加的值和为:{{zongjia}}</p>
    </div>
  </div>
  <script src="../vue.js"></script>
  <script>
    const app = new Vue({
      el: "#app",
      //定义属性
      data: {
        arr: [9, 15, 19, 25, 29, 31, 48, 57, 62, 79, 87],
        zongjia: 0,
        select: []

      },
      //定义方法
      methods: {
        txt(id) {
          if (this.select.includes(this.arr[id])) {
            this.select = this.select.filter(
              (inex) => inex !== this.arr[id]
            );

          } else {
            this.select.push(this.arr[id]);
          }
          this.zongjia = this.select.reduce(function (pre, inex) {
            return inex + pre
          }, 0)
        }
      },
    });
  </script>
</body>

</html>